/*
 * Copyright (c) 2019-2020 Arm Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#ifdef __ARM_FEATURE_SVE

#include <cstdint>
#include "../../asmlib.hpp"

namespace arm_gemm {

void sve_interleaved_s8s32_dot_8x3VL(const int8_t *Apanel, const int8_t *Bpanel, int32_t *Cpanel, int ablocks, int bblocks, int K) {
    const int8_t *a_ptr = Apanel;
    int32_t *c_ptr = Cpanel;

    K /= 4;
    const long loops_count = (K / 2) - 1;
    const long tails_count = K % 2;

    for (int yb=0; yb<ablocks; yb++) {
        const int8_t *a_ptr0 = a_ptr;
        const int8_t *b_ptr = Bpanel;

        for (int xb=0; xb<bblocks; xb++) {
            a_ptr = a_ptr0;
            long loops = loops_count;
            long tails = tails_count;

            __asm __volatile (
                "mov z8.s, #0\n"
                "ptrue p0.b\n"
                "mov z9.s, #0\n"
                "mov z10.s, #0\n"
                "mov z11.s, #0\n"
                "ld1rqb z0.b, p0/z, [%[a_ptr]]\n"
                "mov z12.s, #0\n"
                "ld1b z4.b, p0/z, [%[b_ptr]]\n"
                "mov z13.s, #0\n"
                "ld1rqb z1.b, p0/z, [%[a_ptr], #0x10]\n"
                "mov z14.s, #0\n"
                "ld1b z5.b, p0/z, [%[b_ptr], #1, MUL VL]\n"
                "mov z15.s, #0\n"
                "ld1rqb z2.b, p0/z, [%[a_ptr], #0x20]\n"
                "mov z16.s, #0\n"
                "add %[a_ptr], %[a_ptr], #0x40\n"
                "mov z17.s, #0\n"
                "addvl %[b_ptr], %[b_ptr], #3\n"
                "mov z18.s, #0\n"
                "mov z19.s, #0\n"
                "mov z20.s, #0\n"
                "mov z21.s, #0\n"
                "mov z22.s, #0\n"
                "mov z23.s, #0\n"
                "mov z24.s, #0\n"
                "mov z25.s, #0\n"
                "mov z26.s, #0\n"
                "mov z27.s, #0\n"
                "mov z28.s, #0\n"
                "mov z29.s, #0\n"
                "mov z30.s, #0\n"
                "mov z31.s, #0\n"
                "cbz %[loops], 1f\n"
                "2:\n"
                "sdot z8.s, z4.b, z0.b[0]\n"
                "ld1b z6.b, p0/z, [%[b_ptr], #-1, MUL VL]\n"
                "sdot z9.s, z4.b, z0.b[1]\n"
                "ld1rqb z3.b, p0/z, [%[a_ptr], #-0x10]\n"
                "sdot z10.s, z4.b, z0.b[2]\n"
                "subs %[loops], %[loops], #0x1\n"
                "sdot z11.s, z4.b, z0.b[3]\n"
                "sdot z20.s, z4.b, z1.b[0]\n"
                "sdot z21.s, z4.b, z1.b[1]\n"
                "sdot z22.s, z4.b, z1.b[2]\n"
                "sdot z23.s, z4.b, z1.b[3]\n"
                "ld1b z4.b, p0/z, [%[b_ptr]]\n"
                "sdot z12.s, z5.b, z0.b[0]\n"
                "sdot z13.s, z5.b, z0.b[1]\n"
                "sdot z14.s, z5.b, z0.b[2]\n"
                "sdot z15.s, z5.b, z0.b[3]\n"
                "sdot z24.s, z5.b, z1.b[0]\n"
                "sdot z25.s, z5.b, z1.b[1]\n"
                "sdot z26.s, z5.b, z1.b[2]\n"
                "sdot z27.s, z5.b, z1.b[3]\n"
                "ld1b z5.b, p0/z, [%[b_ptr], #1, MUL VL]\n"
                "sdot z16.s, z6.b, z0.b[0]\n"
                "sdot z17.s, z6.b, z0.b[1]\n"
                "sdot z18.s, z6.b, z0.b[2]\n"
                "sdot z19.s, z6.b, z0.b[3]\n"
                "ld1rqb z0.b, p0/z, [%[a_ptr]]\n"
                "sdot z28.s, z6.b, z1.b[0]\n"
                "sdot z29.s, z6.b, z1.b[1]\n"
                "sdot z30.s, z6.b, z1.b[2]\n"
                "sdot z31.s, z6.b, z1.b[3]\n"
                "ld1b z6.b, p0/z, [%[b_ptr], #2, MUL VL]\n"
                "sdot z8.s, z4.b, z2.b[0]\n"
                "ld1rqb z1.b, p0/z, [%[a_ptr], #0x10]\n"
                "sdot z9.s, z4.b, z2.b[1]\n"
                "add %[a_ptr], %[a_ptr], #0x40\n"
                "sdot z10.s, z4.b, z2.b[2]\n"
                "addvl %[b_ptr], %[b_ptr], #6\n"
                "sdot z11.s, z4.b, z2.b[3]\n"
                "sdot z20.s, z4.b, z3.b[0]\n"
                "sdot z21.s, z4.b, z3.b[1]\n"
                "sdot z22.s, z4.b, z3.b[2]\n"
                "sdot z23.s, z4.b, z3.b[3]\n"
                "ld1b z4.b, p0/z, [%[b_ptr], #-3, MUL VL]\n"
                "sdot z12.s, z5.b, z2.b[0]\n"
                "sdot z13.s, z5.b, z2.b[1]\n"
                "sdot z14.s, z5.b, z2.b[2]\n"
                "sdot z15.s, z5.b, z2.b[3]\n"
                "sdot z24.s, z5.b, z3.b[0]\n"
                "sdot z25.s, z5.b, z3.b[1]\n"
                "sdot z26.s, z5.b, z3.b[2]\n"
                "sdot z27.s, z5.b, z3.b[3]\n"
                "ld1b z5.b, p0/z, [%[b_ptr], #-2, MUL VL]\n"
                "sdot z16.s, z6.b, z2.b[0]\n"
                "sdot z17.s, z6.b, z2.b[1]\n"
                "sdot z18.s, z6.b, z2.b[2]\n"
                "sdot z19.s, z6.b, z2.b[3]\n"
                "ld1rqb z2.b, p0/z, [%[a_ptr], #-0x20]\n"
                "sdot z28.s, z6.b, z3.b[0]\n"
                "sdot z29.s, z6.b, z3.b[1]\n"
                "sdot z30.s, z6.b, z3.b[2]\n"
                "sdot z31.s, z6.b, z3.b[3]\n"
                "b.ne 2b\n"
                "1:\n"
                "cbz %[tails], 3f\n"
                "sdot z8.s, z4.b, z0.b[0]\n"
                "ld1b z6.b, p0/z, [%[b_ptr], #-1, MUL VL]\n"
                "sdot z9.s, z4.b, z0.b[1]\n"
                "ld1rqb z3.b, p0/z, [%[a_ptr], #-0x10]\n"
                "sdot z10.s, z4.b, z0.b[2]\n"
                "sdot z11.s, z4.b, z0.b[3]\n"
                "sdot z20.s, z4.b, z1.b[0]\n"
                "sdot z21.s, z4.b, z1.b[1]\n"
                "sdot z22.s, z4.b, z1.b[2]\n"
                "sdot z23.s, z4.b, z1.b[3]\n"
                "ld1b z4.b, p0/z, [%[b_ptr]]\n"
                "sdot z12.s, z5.b, z0.b[0]\n"
                "sdot z13.s, z5.b, z0.b[1]\n"
                "sdot z14.s, z5.b, z0.b[2]\n"
                "sdot z15.s, z5.b, z0.b[3]\n"
                "sdot z24.s, z5.b, z1.b[0]\n"
                "sdot z25.s, z5.b, z1.b[1]\n"
                "sdot z26.s, z5.b, z1.b[2]\n"
                "sdot z27.s, z5.b, z1.b[3]\n"
                "ld1b z5.b, p0/z, [%[b_ptr], #1, MUL VL]\n"
                "sdot z16.s, z6.b, z0.b[0]\n"
                "sdot z17.s, z6.b, z0.b[1]\n"
                "sdot z18.s, z6.b, z0.b[2]\n"
                "sdot z19.s, z6.b, z0.b[3]\n"
                "ld1rqb z0.b, p0/z, [%[a_ptr]]\n"
                "sdot z28.s, z6.b, z1.b[0]\n"
                "sdot z29.s, z6.b, z1.b[1]\n"
                "sdot z30.s, z6.b, z1.b[2]\n"
                "sdot z31.s, z6.b, z1.b[3]\n"
                "ld1b z6.b, p0/z, [%[b_ptr], #2, MUL VL]\n"
                "sdot z8.s, z4.b, z2.b[0]\n"
                "ld1rqb z1.b, p0/z, [%[a_ptr], #0x10]\n"
                "sdot z9.s, z4.b, z2.b[1]\n"
                "add %[a_ptr], %[a_ptr], #0x20\n"
                "sdot z10.s, z4.b, z2.b[2]\n"
                "addvl %[b_ptr], %[b_ptr], #6\n"
                "sdot z11.s, z4.b, z2.b[3]\n"
                "sdot z20.s, z4.b, z3.b[0]\n"
                "sdot z21.s, z4.b, z3.b[1]\n"
                "sdot z22.s, z4.b, z3.b[2]\n"
                "sdot z23.s, z4.b, z3.b[3]\n"
                "ld1b z4.b, p0/z, [%[b_ptr], #-3, MUL VL]\n"
                "sdot z12.s, z5.b, z2.b[0]\n"
                "sdot z13.s, z5.b, z2.b[1]\n"
                "sdot z14.s, z5.b, z2.b[2]\n"
                "sdot z15.s, z5.b, z2.b[3]\n"
                "sdot z24.s, z5.b, z3.b[0]\n"
                "sdot z25.s, z5.b, z3.b[1]\n"
                "sdot z26.s, z5.b, z3.b[2]\n"
                "sdot z27.s, z5.b, z3.b[3]\n"
                "ld1b z5.b, p0/z, [%[b_ptr], #-2, MUL VL]\n"
                "sdot z16.s, z6.b, z2.b[0]\n"
                "sdot z17.s, z6.b, z2.b[1]\n"
                "sdot z18.s, z6.b, z2.b[2]\n"
                "sdot z19.s, z6.b, z2.b[3]\n"
                "sdot z28.s, z6.b, z3.b[0]\n"
                "sdot z29.s, z6.b, z3.b[1]\n"
                "sdot z30.s, z6.b, z3.b[2]\n"
                "sdot z31.s, z6.b, z3.b[3]\n"
                "ld1b z6.b, p0/z, [%[b_ptr], #-1, MUL VL]\n"
                "sdot z8.s, z4.b, z0.b[0]\n"
                "sdot z9.s, z4.b, z0.b[1]\n"
                "sdot z10.s, z4.b, z0.b[2]\n"
                "sdot z11.s, z4.b, z0.b[3]\n"
                "st1w z8.s, p0, [%[c_ptr]]\n"
                "sdot z20.s, z4.b, z1.b[0]\n"
                "sdot z21.s, z4.b, z1.b[1]\n"
                "sdot z22.s, z4.b, z1.b[2]\n"
                "sdot z23.s, z4.b, z1.b[3]\n"
                "sdot z12.s, z5.b, z0.b[0]\n"
                "sdot z13.s, z5.b, z0.b[1]\n"
                "sdot z14.s, z5.b, z0.b[2]\n"
                "sdot z15.s, z5.b, z0.b[3]\n"
                "st1w z12.s, p0, [%[c_ptr], #1, MUL VL]\n"
                "sdot z24.s, z5.b, z1.b[0]\n"
                "sdot z25.s, z5.b, z1.b[1]\n"
                "sdot z26.s, z5.b, z1.b[2]\n"
                "sdot z27.s, z5.b, z1.b[3]\n"
                "sdot z16.s, z6.b, z0.b[0]\n"
                "sdot z17.s, z6.b, z0.b[1]\n"
                "sdot z18.s, z6.b, z0.b[2]\n"
                "sdot z19.s, z6.b, z0.b[3]\n"
                "st1w z16.s, p0, [%[c_ptr], #2, MUL VL]\n"
                "sdot z28.s, z6.b, z1.b[0]\n"
                "sdot z29.s, z6.b, z1.b[1]\n"
                "sdot z30.s, z6.b, z1.b[2]\n"
                "st1w z9.s, p0, [%[c_ptr], #3, MUL VL]\n"
                "sdot z31.s, z6.b, z1.b[3]\n"
                "b 4f\n"
                "3:\n"
                "sdot z8.s, z4.b, z0.b[0]\n"
                "ld1b z6.b, p0/z, [%[b_ptr], #-1, MUL VL]\n"
                "sdot z9.s, z4.b, z0.b[1]\n"
                "ld1rqb z3.b, p0/z, [%[a_ptr], #-0x10]\n"
                "sdot z10.s, z4.b, z0.b[2]\n"
                "addvl %[b_ptr], %[b_ptr], #3\n"
                "sdot z11.s, z4.b, z0.b[3]\n"
                "sdot z20.s, z4.b, z1.b[0]\n"
                "sdot z21.s, z4.b, z1.b[1]\n"
                "sdot z22.s, z4.b, z1.b[2]\n"
                "sdot z23.s, z4.b, z1.b[3]\n"
                "ld1b z4.b, p0/z, [%[b_ptr], #-3, MUL VL]\n"
                "sdot z12.s, z5.b, z0.b[0]\n"
                "sdot z13.s, z5.b, z0.b[1]\n"
                "sdot z14.s, z5.b, z0.b[2]\n"
                "sdot z15.s, z5.b, z0.b[3]\n"
                "sdot z24.s, z5.b, z1.b[0]\n"
                "sdot z25.s, z5.b, z1.b[1]\n"
                "sdot z26.s, z5.b, z1.b[2]\n"
                "sdot z27.s, z5.b, z1.b[3]\n"
                "ld1b z5.b, p0/z, [%[b_ptr], #-2, MUL VL]\n"
                "sdot z16.s, z6.b, z0.b[0]\n"
                "sdot z17.s, z6.b, z0.b[1]\n"
                "sdot z18.s, z6.b, z0.b[2]\n"
                "sdot z19.s, z6.b, z0.b[3]\n"
                "sdot z28.s, z6.b, z1.b[0]\n"
                "sdot z29.s, z6.b, z1.b[1]\n"
                "sdot z30.s, z6.b, z1.b[2]\n"
                "sdot z31.s, z6.b, z1.b[3]\n"
                "ld1b z6.b, p0/z, [%[b_ptr], #-1, MUL VL]\n"
                "sdot z8.s, z4.b, z2.b[0]\n"
                "sdot z9.s, z4.b, z2.b[1]\n"
                "sdot z10.s, z4.b, z2.b[2]\n"
                "sdot z11.s, z4.b, z2.b[3]\n"
                "st1w z8.s, p0, [%[c_ptr]]\n"
                "sdot z20.s, z4.b, z3.b[0]\n"
                "sdot z21.s, z4.b, z3.b[1]\n"
                "sdot z22.s, z4.b, z3.b[2]\n"
                "sdot z23.s, z4.b, z3.b[3]\n"
                "sdot z12.s, z5.b, z2.b[0]\n"
                "sdot z13.s, z5.b, z2.b[1]\n"
                "sdot z14.s, z5.b, z2.b[2]\n"
                "sdot z15.s, z5.b, z2.b[3]\n"
                "st1w z12.s, p0, [%[c_ptr], #1, MUL VL]\n"
                "sdot z24.s, z5.b, z3.b[0]\n"
                "sdot z25.s, z5.b, z3.b[1]\n"
                "sdot z26.s, z5.b, z3.b[2]\n"
                "sdot z27.s, z5.b, z3.b[3]\n"
                "sdot z16.s, z6.b, z2.b[0]\n"
                "sdot z17.s, z6.b, z2.b[1]\n"
                "sdot z18.s, z6.b, z2.b[2]\n"
                "sdot z19.s, z6.b, z2.b[3]\n"
                "st1w z16.s, p0, [%[c_ptr], #2, MUL VL]\n"
                "sdot z28.s, z6.b, z3.b[0]\n"
                "sdot z29.s, z6.b, z3.b[1]\n"
                "sdot z30.s, z6.b, z3.b[2]\n"
                "st1w z9.s, p0, [%[c_ptr], #3, MUL VL]\n"
                "sdot z31.s, z6.b, z3.b[3]\n"
                "4:\n"
                "st1w z13.s, p0, [%[c_ptr], #4, MUL VL]\n"
                "st1w z17.s, p0, [%[c_ptr], #5, MUL VL]\n"
                "st1w z10.s, p0, [%[c_ptr], #6, MUL VL]\n"
                "st1w z14.s, p0, [%[c_ptr], #7, MUL VL]\n"
                "addvl %[c_ptr], %[c_ptr], #16\n"
                "st1w z18.s, p0, [%[c_ptr], #-8, MUL VL]\n"
                "st1w z11.s, p0, [%[c_ptr], #-7, MUL VL]\n"
                "st1w z15.s, p0, [%[c_ptr], #-6, MUL VL]\n"
                "st1w z19.s, p0, [%[c_ptr], #-5, MUL VL]\n"
                "st1w z20.s, p0, [%[c_ptr], #-4, MUL VL]\n"
                "st1w z24.s, p0, [%[c_ptr], #-3, MUL VL]\n"
                "st1w z28.s, p0, [%[c_ptr], #-2, MUL VL]\n"
                "st1w z21.s, p0, [%[c_ptr], #-1, MUL VL]\n"
                "st1w z25.s, p0, [%[c_ptr]]\n"
                "st1w z29.s, p0, [%[c_ptr], #1, MUL VL]\n"
                "st1w z22.s, p0, [%[c_ptr], #2, MUL VL]\n"
                "st1w z26.s, p0, [%[c_ptr], #3, MUL VL]\n"
                "st1w z30.s, p0, [%[c_ptr], #4, MUL VL]\n"
                "st1w z23.s, p0, [%[c_ptr], #5, MUL VL]\n"
                "st1w z27.s, p0, [%[c_ptr], #6, MUL VL]\n"
                "st1w z31.s, p0, [%[c_ptr], #7, MUL VL]\n"
                "addvl %[c_ptr], %[c_ptr], #8\n"
            : [a_ptr] "+r" (a_ptr), [b_ptr] "+r" (b_ptr), [c_ptr] "+r" (c_ptr),
              [loops] "+r" (loops), [tails] "+r" (tails)
            :
            : "z0", "z1", "z2", "z3", "z4", "z5", "z6", "z8", "z9", "z10", "z11", "z12", "z13", "z14", "z15", "z16", "z17", "z18", "z19", "z20", "z21", "z22", "z23", "z24", "z25", "z26", "z27", "z28", "z29", "z30", "z31", "cc", "memory"
            );
        }
    }
}

} // namespace arm_gemm

#endif // __ARM_FEATURE_SVE
